package com.simin.indexrecyclerview;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recy_list)
    RecyclerView recyList;
    @BindView(R.id.fastIndexView)
    FastIndexView fastIndexView;
    //主要用于展示数据的list
    private List<CityInfoModel> list;
    //第一次加载之后缓存的数据
    private List<CityInfoModel> cacheList;

    //页面recyclerview的适配器
    private CityAdapter mainAdapter;
    //布局管理器
    private LinearLayoutManager layoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initAdapter();

        initListener();
    }

    private void initAdapter() {
        list = new ArrayList<>();
        cacheList = new ArrayList<>();
        list.add(new CityInfoModel("安徽省"));
        list.add(new CityInfoModel("北京市"));
        list.add(new CityInfoModel("上海市"));
        list.add(new CityInfoModel("广州市"));
        list.add(new CityInfoModel("深圳市"));
        list.add(new CityInfoModel("天津市"));
        list.add(new CityInfoModel("南京市"));
        list.add(new CityInfoModel("杭州市"));
        list.add(new CityInfoModel("重庆市"));
        list.add(new CityInfoModel("成都市"));
        list.add(new CityInfoModel("石家庄市"));
        list.add(new CityInfoModel("自贡市"));
        list.add(new CityInfoModel("攀枝花市"));
        list.add(new CityInfoModel("泸州市"));
        list.add(new CityInfoModel("德阳市"));
        list.add(new CityInfoModel("绵阳市"));
        list.add(new CityInfoModel("广元市"));
        list.add(new CityInfoModel("遂宁市"));
        List<CityInfoModel> cityInfoModels = bindData(list);
        layoutManager = new LinearLayoutManager(this);
        recyList.setLayoutManager(layoutManager);
        recyList.addItemDecoration(new CustomItemDecoration(this, new CustomItemDecoration.TitleDecorationCallback() {
            @Override
            public String getGroupId(int position) {
                //这个是用来比较是否是同一组数据的
                return list.get(position).getSortId();
            }

            @Override
            public String getGroupName(int position) {
                CityInfoModel cityInfoModel = list.get(position);
                //拼音都是小写的
                return cityInfoModel.getSortId().toUpperCase();
            }
        }));
        mainAdapter = new CityAdapter(this, cityInfoModels);
        recyList.setAdapter(mainAdapter);
    }

    private void initListener() {
        fastIndexView.setListener(new FastIndexView.OnLetterUpdateListener() {
            @Override
            public void onLetterUpdate(String letter) {
                moveToLetterPosition(letter);
            }
        });
    }

    //滚动recyclerview
    private void moveToLetterPosition(String letter) {
        //这里主要是为了跳转到最顶端
        if ("#".equals(letter)) {
            letter = "*";
        }
        for (int i = 0; i < list.size(); i++) {
            CityInfoModel cityInfoModel = list.get(i);
            if (cityInfoModel.getSortId().toUpperCase().equals(letter)) {
                layoutManager.scrollToPositionWithOffset(i, 0);
                return;
            }
        }
    }

    /**
     * 给View绑定数据
     *
     * @param allCity 所有城市列表
     */
    public List<CityInfoModel> bindData(List<CityInfoModel> allCity) {
        if (allCity != null) {
            for (CityInfoModel cityModel : allCity) {
                try {
                    String pingYin = PinyinHelper.convertToPinyinString(cityModel.getCityName(), " ", PinyinFormat.WITHOUT_TONE);
                    cacheList.add(new CityInfoModel(cityModel.getCityName(), pingYin.substring(0, 1), pingYin));
                } catch (PinyinException e) {
                    e.printStackTrace();
                }
            }
            //排序
            Collections.sort(cacheList, new Comparator<CityInfoModel>() {
                @Override
                public int compare(CityInfoModel o1, CityInfoModel o2) {
                    return o1.getSortName().compareTo(o2.getSortName());
                }
            });
            this.list.clear();
            this.list.addAll(cacheList);
        }
        return list;
    }

}
